package com.vieewer.btdemo.utils;

import android.annotation.SuppressLint;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
import com.vieewer.btdemo.app.App;

import static android.content.Context.KEYGUARD_SERVICE;

public class ScreenSleepUtil {

    private PowerManager.WakeLock mWakeLock;

    /**
     * 设置休眠时间
     *
     * @param millisecond
     * @param context
     */
    public static void setScreenSleepTime(int millisecond, Context context) {
        try {
            Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT,
                    millisecond);
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    /**
     * @param context
     * @return
     */
    public static int getScreenSleepTime(Context context) {
        int millisecond = 0;
        try {
            millisecond = Settings.System.getInt(context.getContentResolver(),
                    Settings.System.SCREEN_OFF_TIMEOUT);
        } catch (Exception localException) {

        }
        return millisecond;
    }



    /**
     * 唤醒手机屏幕并解锁
     */
    public static void wakeUpAndUnlock() {
        Log.i("yjj", "唤醒手机屏幕并解锁");
        PowerManager pm = (PowerManager) App.instance.getSystemService(Context.POWER_SERVICE);
        @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                        PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "wakup");
        wl.acquire(10000);
        wl.release();

        KeyguardManager keyguardManager = (KeyguardManager)App.instance
                .getSystemService(KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");

        keyguardLock.reenableKeyguard();
        keyguardLock.disableKeyguard();
    }

    /**
     * 判断当前屏幕是否亮
     *
     * @return
     */
    public static boolean isScreenOn() {
        PowerManager pm = (PowerManager) App.instance.getSystemService(Context.POWER_SERVICE);
        boolean screenOn = pm.isScreenOn();

        return screenOn;
    }



    //禁止屏幕休眠, 添加休眠锁，休眠锁必须成对出现。
    public void acquireWakeLock(Context context) {
        if(mWakeLock == null) {
            PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
                    this.getClass().getCanonicalName());
            mWakeLock.acquire();

        }

    }

    public void releaseWakeLock() {
        if(mWakeLock != null) {
            mWakeLock.release();
            mWakeLock = null;
        }
    }


    //2、在Activity布局中设置Windows属性进行控制，此种方法的局限性是只有在Activity类中有效，Activity退出时失效。
    //getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);



}
